using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Sandia.MVCFramework.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace Sandia.MVCFramework.View
{
    /// <summary>
    /// Custom trace listener that provides an MVC Framework
    /// View of log entries
    /// </summary>
    [ConfigurationElementType(typeof(CustomTraceListenerData))]
    public class LoggingViewTraceListener : CustomTraceListener
    {
        StringBuilder message = new StringBuilder();

        /// <summary>
        /// Writes trace information, a data object and event information to the listener specific output.
        /// </summary>
        /// <param name="eventCache">A TraceEventCache object that contains the current process ID, thread ID, and stack trace information</param>
        /// <param name="source">A name used to identify the output, typically the name of the application that generated the trace event</param>
        /// <param name="eventType">One of the TraceEventType values specifying the type of event that has caused the trace</param>
        /// <param name="id">A numeric identifier for the event</param>
        /// <param name="data">The trace data to emit</param>
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            List<LoggingView> loggingViews = StandardAppOptions.GetLoggingViews();
            if (loggingViews != null)
            {
                LogEntry logEntry = data as LogEntry;
                foreach (LoggingView loggingView in loggingViews)
                {
                    if (logEntry != null && this.Formatter != null)
                    {
                        loggingView.AddLogEntry(this.Formatter.Format(logEntry));
                    }
                    else
                    {
                        loggingView.AddLogEntry(data.ToString());
                    }
                }
            }
        }

        /// <summary>
        /// Writes the specified message to the listener
        /// </summary>
        /// <param name="message">The message to write</param>
        public override void Write(string message)
        {
            this.message.Append(message);
        }

        /// <summary>
        /// Writes the specified message to the listener
        /// </summary>
        /// <param name="message">The message to write</param>
        public override void WriteLine(string message)
        {
            this.message.Append(message);
            List<LoggingView> loggingViews = StandardAppOptions.GetLoggingViews();
            foreach (LoggingView loggingView in loggingViews)
            {
                if (loggingViews != null)
                {
                    loggingView.AddLogEntry(message);
                }
            }
            this.message = new StringBuilder();
        }
    }
}
